<?php

namespace console\controllers;

use common\models\statistics\ManageReport;
use Yii;
use yii\console\Controller;
use Exception;

/**
 * 经营报表数据定时 快照
 *
 * Class ManagementReportController
 * @package console\controllers
 */
class ManagementReportController extends Controller
{

    /**
     * @author wangxiang
     * @param string $date
     * @param int $projectId
     */
    public function actionIndex(string $date = '', int $projectId = 0)
    {
        $sTime = time();
        $time = $date ? strtotime($date) : strtotime(date('Y-m-d', strtotime("-1 day")));
        try {
            Yii::$app->services->statisticsManagementReport->generateSnapshot($time, $projectId);
        } catch (Exception $exception) {
            // 记录日志
            $webHook = 'https://oapi.dingtalk.com/robot/send?access_token=8c603ecf0d473b25aff945f6d08c454295ff029d3bb8fb26292b200b2d519d20';
            Yii::$app->services->statisticsManagementReport->dingMsg($webHook, $exception);
            echo 'error:' . $exception;
        }

        echo date('Y-m-d H:i:s') . ' - success！' . PHP_EOL;
    }


    /**
     * 修复挂号数据
     *
     * @author wangxiang
     * @param int $projectId 项目id
     * @param string $date 日期 默认昨天
     * @param int $type 维度
     */
    public function actionRepair(int $projectId, string $date = '', int $type = ManageReport::TYPE_DAY)
    {
        $time = $date ? strtotime($date) : strtotime(date('Y-m-d', strtotime("-1 day")));
        try {
            Yii::$app->services->statisticsManagementReport->repairSnapshot($projectId, $type, $time);
            echo date('Y-m-d H:i:s') . ' - success！' . PHP_EOL;
        } catch (Exception $exception) {
            echo 'error:' . $exception;
        }
    }
}
